package com.indusfo.spc.service.impl;


import com.indusfo.spc.mapper.DeviceTypeInspectMapper;
import com.indusfo.spc.mapper.DeviceTypeMaintainMapper;
import com.indusfo.spc.mapper.DeviceTypeMapper;
import com.indusfo.spc.pojo.DeviceType;
import com.indusfo.spc.pojo.DeviceTypeInspect;
import com.indusfo.spc.pojo.DeviceTypeMaintain;
import com.indusfo.spc.service.DeviceTypeMaintainService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName DeviceTypeInspectServiceImpl
 * @Description 设备类型保养项目service
 * @Author louk
 * @Date 2019/12/11 8:50
 * @Version 1.0
 */
@Service
public class DeviceTypeMaintainServiceImpl implements DeviceTypeMaintainService {

    private  static  final Logger logger = LoggerFactory.getLogger(DeviceTypeMaintainServiceImpl.class);

    @Resource
    DeviceTypeMaintainMapper deviceTypeMaintainMapper;
    @Resource
    DeviceTypeMapper deviceTypeMapper;
    /**
     * 批量新增设备类型保养项目明细
     * @param maintainIds
     * @param deviceTypeId
     * @return
     */
    @Override
    public JSONObject updateDeviceTypeMaintainList(Integer[] maintainIds, Integer deviceTypeId) {
        if(maintainIds.length==0 || maintainIds==null){
            return JSONObject.build(JSONObject.ERROR,"未选择保养项目");
        }
        if(deviceTypeId==null || "".equals(deviceTypeId)){
            //获取所有的物料类别进行构建树
            List<DeviceType> list = deviceTypeMapper.queryAllDeviceType(new DeviceType());
            deviceTypeId=list.get(0).getDeviceTypeId();
        }
        int updateCount = deviceTypeMaintainMapper.updateDeviceTypeMaintainList(maintainIds, deviceTypeId);
        if(updateCount>0){
            return JSONObject.build(JSONObject.SUCCESS,"新增成功");
        }
        return JSONObject.build(JSONObject.ERROR,"新增失败");
    }

    /**
     * 删除设备类型保养项目明细
     * @param deviceTypeMaintainId
     * @return
     */
    @Override
    public JSONObject defineDeviceTypeMaintain(Integer deviceTypeMaintainId, Integer dataState) {
        if(deviceTypeMaintainId==null || "".equals(deviceTypeMaintainId)){
            return JSONObject.build(JSONObject.ERROR,"未选择设备类型点检项目");
        }

        int defineCount = deviceTypeMaintainMapper.defineDeviceTypeMaintain(deviceTypeMaintainId, dataState);
        if(defineCount>0){
                return JSONObject.build(JSONObject.SUCCESS,"删除成功");
        }
        return JSONObject.build(JSONObject.ERROR,"删除失败");
    }

    /**
     * 批量删除设备类型保养项目明细
     * @param deviceTypeMaintainIds
     * @return
     */
    @Override
    public JSONObject defineDeviceTypeMaintainList(Integer[] deviceTypeMaintainIds, Integer dataState) {
        if(deviceTypeMaintainIds==null || deviceTypeMaintainIds.length==0) {
            return JSONObject.build(JSONObject.ERROR, "未选择设备类型保养项目");
        }
        int defineCount = deviceTypeMaintainMapper.defineDeviceTypeMaintainList(deviceTypeMaintainIds, dataState);
        if(defineCount>0){
            return JSONObject.build(JSONObject.SUCCESS,"删除成功");
        }
        return JSONObject.build(JSONObject.ERROR,"删除失败");
    }
    /**
     * 查询设备类型保养项目明细
     * @param deviceTypeMaintain
     * @return
     */
    @Override
    public JSONObject queryDeviceTypeMaintainList(DeviceTypeMaintain deviceTypeMaintain) {

        Integer pagesize = deviceTypeMaintain.getPagesize();
        Integer pageindex = deviceTypeMaintain.getPageindex();
        if(pagesize != null && pageindex != null) {
            deviceTypeMaintain.setIncept(pagesize*(pageindex - 1));
        }
        /*Integer deviceTypeId = deviceTypeMaintain.getDeviceTypeId();
        if(deviceTypeId==null || "".equals(deviceTypeId)){
            //获取所有的物料类别进行构建树
            List<DeviceType> list = deviceTypeMapper.queryAllDeviceType(new DeviceType());
            if(list.isEmpty()){
                return JSONObject.oK("暂无数据");

            }
            deviceTypeMaintain.setDeviceTypeId(list.get(0).getDeviceTypeId());
        }*/
        //查询该设备的保养明细
        List<DeviceTypeMaintain> deviceTypeMaintainList = deviceTypeMaintainMapper.queryDeviceTypeMaintainList(deviceTypeMaintain);
        if(!deviceTypeMaintainList.isEmpty()){
            int listCount = deviceTypeMaintainMapper.countDeviceTypeMaintainList(deviceTypeMaintain);
            return JSONObject.oK("查询成功",deviceTypeMaintainList,listCount);
        }
        return JSONObject.oK("没有相关数据",deviceTypeMaintainList,null);
    }
}
